(function(factory) {
	if (typeof define === "function" && define.amd) {

		// AMD. Register as an anonymous module.
		define(['HteOS/window/Window'], factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	/**
	 * @author 李球 <service@hteos.com>
	 * @class HteOS.window.WindowManager 应用窗口管理器
	 * @singleton
	 */
	HteOS.window.WindowManager = {

		/**
		 * 所有窗口集合
		 *
		 * @property
		 */
		windows: {},

		/**
		 * 窗口初始Z-INDEX
		 *
		 * @property
		 */
		zIndex: 1003,

		/**
		 * 窗口层级数组
		 *
		 * @property
		 */
		zIndexs: [],

		/**
		 * 初始化窗口管理器，在这个方法中监听了窗口组件的相关事件
		 */
		init: function() {
			var me = this;
			HteOS.EventManager.on("hte.window.create", function( win) {
				me.windows[win.id] = win;
			});

			HteOS.EventManager.on("hte.window.show", function(win) {
				me.setActive(win.id);
			});

			HteOS.EventManager.on("hte.window.close", function( win) {
				me.closeWindow(win.id);
			});

			HteOS.EventManager.on("hte.window.hide", function( win) {
				me.removeZIndex(win.id);
				me.updateActive();
			});

			this.bindControlEvent();
			this.initConextMenu();
		},
		
		initConextMenu :function(){
			var me = this;
			new HteOS.menu.Menu({
				selector: ".hte-window-header",
				cls: "hte-window-menu",
				items: [{
					id: 'restore',
					icon: 'fa fa-window-restore',
					name: '还原',
					handler: function() {
						me.getActive().restore();
					}
				},{
					id :'hide',
					icon : 'fa fa-window-minimize',
					name :'最小化',
					handler :function(){
						me.getActive().hide();
					}
				},{
					id :'maximize',
					icon : 'fa fa-window-maximize',
					name :'最大化',
					handler :function(){
						me.getActive().maximize();
					}
				},'-',{
					id :'close',
					icon : 'fa fa-window-close-o',
					name :'关闭',
					handler :function(){
						me.getActive().close();
					}
				}],
				events: {
					show: function(target) {
						var me = this,win = $(target).parents(".hte-window");
						win = HteOS.window.WindowManager.get(win.attr("id"));
						//当前选中的窗口展示在最前前面
						win.front();
						//如果最大化，则将最大化按钮停用，重置按钮启用
						if (win.maximized === true) {
							me.disable("maximize");
							me.enable("restore");
						} else {
							//没有最大化
							me.enable("maximize");
							me.disable("restore");
						}
						if(win.maximizable !== true){
							me.disable("maximize");
						}
					}
				}
			});
		},

		bindControlEvent: function() {
			var doc = $(document),me = this;
			// 关闭按钮
			doc.on('click.hte.window.data-api', '.btn-close', function(event) {
				var win = me.getWindowByTarget(event.target);
				//win.close();
				HteOS.TaskManager.stop(win.taskId);
				event.stopPropagation();
			});

			// 重置按钮
			doc.on('click.hte.window.data-api', '.btn-restore', function(event) {
				var win = me.getWindowByTarget(event.target);
				win.restore();
				event.stopPropagation();
			});

			// 双击表头
			doc.on('dblclick.hte.window.data-api', '.hte-window-header',
				function(event) {
					var win = me.getWindowByTarget(event.target);
					if(win.maximizable == true){
						win.toggleSize();
					}
					event.stopPropagation();
					event.preventDefault();
				});

			// 单击表头
			doc.on('click.hte.window.data-api', '.hte-window', function(event) {
				var win = me.getWindowByTarget(event.target);
				win.front();
			});

			// 最大化窗口
			doc.on('click.hte.window.data-api', '.btn-max', function(event) {
				var win = me.getWindowByTarget(event.target);
				win.maximize();
				event.stopPropagation();
			});

			// 最小化窗口
			doc.on('click.hte.window.data-api', '.btn-min', function(event) {
				var win = me.getWindowByTarget(event.target);
				win.hide();
				event.stopPropagation();
			});
		},

		getWindowByTarget: function(target) {
			var target = $(target);
			if (!target.hasClass(".hte-window")) {
				target = target.parents(".hte-window");
			}
			return this.get(target.attr("id"));
		},

		/**
		 * 关闭窗口
		 *
		 * @param {String}
		 *            id
		 */
		closeWindow: function(id) {
			var me = this,
				win = me.windows[id];
			if (me.activeWindow == win) {
				me.activeWindow = null;
			}
			delete me.windows[id];
		},

		/**
		 * 移除窗口层
		 *
		 * @param {String}
		 *            id
		 */
		removeZIndex: function(id) {
			var me = this;
			me.zIndexs.remove(id);
		},

		/**
		 * 设置窗口的Z-INDEX
		 *
		 * @param {String}
		 *            id
		 */
		setZIndex: function(id) {
			var me = this,
				win = me.windows[id];
			win.getEl().css("z-index", me.zIndex++);
			me.removeZIndex(win.id);
			me.zIndexs.push(id);
		},

		/**
		 * 激活窗口
		 *
		 * @param {String}
		 *            id
		 */
		setActive: function(id) {
			var me = this,
				win = me.get(id);
			me.setZIndex(id);
			win.active = true;
			me.updateActive(id);
		},

		/**
		 * 更新桌面活动窗口
		 * @param {String}
		 *            id
		 */
		updateActive: function(id) {
			var me = this,
				activeWindow;
			if (id) {
				activeWindow = me.windows[id];
			} else {
				activeWindow = me.getFront();
			}
			
			if (activeWindow == me.activeWindow) {
				return;
			}
			if (me.activeWindow) {
				/**
				 * @event hte-window.deactivate
				 * @param {HteOS.window.Window} activeWindow
				 * */
				HteOS.EventManager.trigger("hte.window.deactivate",
					me.activeWindow);
			}

			if (me.activeWindow && activeWindow) {
				var el = me.activeWindow.getEl();
				me.activeWindow.active = false;
				el.removeClass("hte-window-active").addClass("inactive");
			}else{
				me.activeWindow = null;
			}
			
			if (activeWindow) {
				var el = activeWindow.getEl();
				el.addClass("hte-window-active").removeClass("inactive");
				activeWindow.active = true;
				HteOS.EventManager
					.trigger("hte.window.activate", activeWindow);
				me.activeWindow = activeWindow;
			}
		},

		/**
		 * 获取当前桌面的最前面的窗口
		 */
		getFront: function() {
			var me = this;
			return me.windows[me.zIndexs[me.zIndexs.length - 1]];
		},
		
		/**
		 *获取当前桌面的活动窗口
		 */
		getActive :function(){
			return this.activeWindow;
		},

		/**
		 * 通过ID获取窗口实例
		 * @param {String} id
		 */
		get: function(id) {
			return this.windows[id];
		}

	}
}));